package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Permission;
import cn.wolfcode.crm.query.PageResult;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IPermissionService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.PermissionName;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("permission")
public class PermissionController {
    @Autowired
    private IPermissionService permissionService;


    @RequiresPermissions("permission:index")
    @PermissionName("权限主页")
    @RequestMapping("")
    public String index(){
        return "permission/list";
    }

    @RequiresPermissions("permission:list")
    @PermissionName("权限数据")
    @RequestMapping("list")
    @ResponseBody
    public PageResult list(QueryObject qo){
        List<Permission> list = permissionService.selectAll();
        PageResult page = new PageResult();
        page.setTotal(list.size());
        page.setRows(list);
        return page;
    }

    @RequestMapping("queryPermissions")
    @ResponseBody
    public List<Permission> queryPermissions(QueryObject qo){
        return permissionService.selectAll();
    }


    @RequiresPermissions("permission:reload")
    @PermissionName("权限加载")
    @RequestMapping("reload")
    @ResponseBody
    public JsonResult reload(){
        JsonResult result = new JsonResult();
        try {
            permissionService.reload();
        }catch (Exception e){
            e.printStackTrace();
            result.markMsg("加载权限失败");
        }
        return result;
    }

    @RequestMapping("queryPermissionByRoleId")
    @ResponseBody
    public List<Permission> queryPermissionByRoleId(Long roleId){
        return permissionService.queryPermissionByRoleId(roleId);
    }

}
